package com.slaffka.services;

import com.slaffka.model.Page;
import com.slaffka.model.PageType;
import org.apache.log4j.Logger;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * _User: v.tsapaev
 * Date: 18.02.12
 * Time: 13:40
 * To change this template use File | Settings | File Templates.
 */
@Service
public class PageService /*implements _PageService*/ {

    private static Logger LOG = Logger.getLogger(PageService.class);
//    @Autowired
//    private PageMapper pageMapper;
//
//    @Override
//    public void setPageMapper(PageMapper pageMapper) {
//        this.pageMapper = pageMapper;
//    }

//    @Override
//    public List<Page> getAllApplicationPages() {
//        return pageMapper.getAllApplicationPages();  //To change body of implemented methods use File | Settings | File Templates.
//    }
//
//    @Override
//    public Page getPageByUrl(String url) {
//        return pageMapper.getPageByUrl(url);
//    }
    @Autowired
    SessionFactory sessionFactory;

    @Transactional
    @CacheEvict(value = {"AppPages", "AdminPages"}, allEntries = true)
    public Page createPage(Page page) {
        sessionFactory.getCurrentSession().save(page);
        return page;
    }

    @Transactional
    @Cacheable(value = "AppPages")
    public List<Page> getAllApplicationPages() {
        return sessionFactory.getCurrentSession()
                .createQuery("from Page where pageType = :pageType")
                .setParameter("pageType", PageType.APPLICATION)
                .list();  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Transactional
    public Page getPageByUrl(String url) {
        return (Page)sessionFactory.getCurrentSession()
                .createQuery("from Page where url = :url")
                .setParameter("url", url)
                .uniqueResult();  //To change body of implemented methods use File | Settings | File Templates.
    }
    
    @Transactional
    public Page getPageById(String pageId) {
        return (Page)sessionFactory.getCurrentSession().get(Page.class, Integer.parseInt(pageId));

    }

    @Transactional
    @Cacheable("AdminPages")
    public List<Page> getAllAdminPages() {
        return (List<Page>)sessionFactory.getCurrentSession()//.createCriteria(Page.class)
                .createQuery("from Page where pageType = :pageType")
                .setParameter("pageType", PageType.ADMIN)               
                .list();
    }

    @Transactional
    @CacheEvict(value = {"AppPages", "AdminPages"}, allEntries = true)
    public Page updatePage(Page page) {
        sessionFactory.getCurrentSession()
                .update(page);
        return page;
    }

    @Transactional
    @CacheEvict(value = {"AppPages", "AdminPages"}, allEntries = true)
    public Boolean removePage(String pageId) {
        Page page = this.getPageById(pageId);
        sessionFactory.getCurrentSession().delete(page);
        return Boolean.TRUE;
    }
}
